package ru.sushka.lunch.dao.psql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {
    
    public static ConnectionManager instance;
    
    private static Connection connection;
    //TODO: properties!
    final static String JDBSDriver = "org.postgresql.Driver";
    final static String url = "jdbc:postgresql://127.0.0.1:5432/LunchDB";
    final static String user = "lunchuser";
    final static String password = "lunchpasswd";
    
    public static ConnectionManager getInstance(){
        if(instance == null){
            instance = new ConnectionManager();
        }
        return instance;
    }
    
    private ConnectionManager(){
        super();
    }
        
    private void open() throws SQLException{
        try {
            Class.forName(JDBSDriver);
            connection = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }    
    }
    
    public Connection getConnection() throws SQLException{
        if(connection == null){
            open();
        }
        return connection;
    }
    
    public void close() throws SQLException {
        connection.close();
    }

}
